package org.gten.gegedexin.service;

import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import org.gten.gegedexin.pojo.DraftPojo;
import org.gten.gegedexin.pojo.MailInfoPojo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

@Service
public class ReadDraftList {
    @Autowired
    DraftServiceImpl draftService;
    JSONArray jsonArray = new JSONArray();
    public void readList(String sender){
        List<DraftPojo> DraftList = draftService.getDraftBySender(sender);
        JSONObject json = new JSONObject();
        for(DraftPojo draft:DraftList){
            json.put("draftID",draft.getDraftId());
            json.put("sender",draft.getSender());
            json.put("receiver",draft.getReceiver());
            json.put("hender",draft.getHeader());
            json.put("editTime",draft.getEditTime().toString());
            jsonArray.add(json);
        }
    }

    public JSONArray readPage(int page){
        JSONArray jsonPage = new JSONArray();
        for(int i=0+(page-1)*10;i<page*10;i++){
            if(i>=jsonArray.size())break;
            jsonPage.add(jsonPage.get(i));
        }
        return jsonPage;
    }

    public int returnPage(){
        return (jsonArray.size()/10) + 1;
    }

}
